/*
 *
 *  Copyright 2019-2022 felord.cn
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *  Website:
 *       https://felord.cn
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package io.github.lhdxhl.wechatpay.v3;

import lombok.Data;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.Objects;

/**
 * @title WxResponseEntity.java
 * @description 请求成功回调类
 * @author lsk
 * @updateTime 2022/9/21 20:19
 * @throws
 */
@Data
public class WxResponseEntity<T> {
    /**
     * The Http status.
     */
    private int httpStatus;
    /**
     * The Body.
     */
    private T body;


    /**
     * @title convert
     * @description 自定义校验
     * @author lsk
     * @param: wxResponseEntity
     * @updateTime 2022/10/1 21:28
     * @throws
     */
    public void convert(WxResponseEntity wxResponseEntity) {
//        if (log.isDebugEnabled()) {
//            log.debug("wechat response {}", responseEntity);
//        }
        if (Objects.nonNull(wxResponseEntity)) {
            this.httpStatus = wxResponseEntity.getHttpStatus();
            this.body = (T) wxResponseEntity.getBody();
        } else {
            this.httpStatus = HttpStatus.REQUEST_TIMEOUT.value();
            this.body = null;
        }
    }


    /**
     * Is 1 xx informational boolean.
     *
     * @return the boolean
     */
    public boolean is1xxInformational() {
//        if (log.isDebugEnabled()) {
//            log.debug("wechat httpStatus {}", this.httpStatus);
//        }
        return HttpStatus.valueOf(this.httpStatus).is1xxInformational();
    }

    /**
     * Is 2xx successful boolean.
     *
     * @return the boolean
     */
    public boolean is2xxSuccessful() {
//        if (log.isDebugEnabled()) {
//            log.debug("wechat httpStatus {}", this.httpStatus);
//        }
        return HttpStatus.valueOf(this.httpStatus).is2xxSuccessful();
    }

    /**
     * Is 3xx redirection boolean.
     *
     * @return the boolean
     */
    public boolean is3xxRedirection() {
//        if (log.isDebugEnabled()) {
//            log.debug("wechat httpStatus {}", this.httpStatus);
//        }
        return HttpStatus.valueOf(this.httpStatus).is3xxRedirection();
    }

    /**
     * Is 4xx client error boolean.
     *
     * @return the boolean
     */
    public boolean is4xxClientError() {
//        if (log.isDebugEnabled()) {
//            log.debug("wechat httpStatus {}", this.httpStatus);
//        }
        return HttpStatus.valueOf(this.httpStatus).is4xxClientError();
    }

    /**
     * Is 5xx server error boolean.
     *
     * @return the boolean
     */
    public boolean is5xxServerError() {
//        if (log.isDebugEnabled()) {
//            log.debug("wechat httpStatus {}", this.httpStatus);
//        }
        return HttpStatus.valueOf(this.httpStatus).is5xxServerError();
    }
}
